Effective Visualizations

Now that you know how to create graphics and visualizations in R, you are armed with powerful tools for scientific computing and analysis. With this power also comes great responsibility. Effective visualizations is an incredibly important aspect of scientific research and communication. There have been several books (see references) written about these principles. In class today we will be going through several case-studies trying to develop some expertise into making effective visualizations.

Worksheet

The worksheet questions for today are embedded into the class notes.

You can download this Rmd file here

Note, there will be very little coding in-class today, but I’ve given you plenty of exercises in the form of a supplemental worksheet (linked at the bottom of this page) to practice with after class is over.

Resources

  1. Fundamentals of Data Visualization by Claus Wilke.

  2. Visualization Analysis and Design by Tamara Munzner.

  3. STAT545.com - Effective Graphics by Jenny Bryan.

  4. ggplot2 book by Hadley Wickam.

  5. Callingbull.org by Carl T. Bergstrom and Jevin West.

Part 1: Warm-up and pre-test [20 mins]

Warmup:

Write some notes here about what “effective visualizations” means to you. Think of elements of good graphics and plots that you have seen - what makes them good or bad? Write 3-5 points.

  1. Clearly labelled
  2. Visually intuitive and simple to understand
  3. Clear messaging
  4. Accurate; not misleading

CQ01: Weekly hours for full-time employees

Question: Evaluate the strength of the claim based on the data: “German workers are more motivated and work more hours than workers in other EU nations.”

Very strong, strong, weak, very week, do not know

  • Weak - double-barrelled question, can’t tell from graph

  • Main takeaway: Summarize the main takeaway from this question/discussion here

CQ02: Average Global Temperature by year

Question: For the years this temperature data is displayed, is there an appreciable increase in temperature?

Yes, No, Do not know

  • Do not know. Can’t see the data because it’s too zoomed out.

  • Main takeaway: Summarize the main takeaway from this question/discussion here

CQ03: Gun deaths in Florida

Question: Evaluate the strength of the claim based on the data: “Soon after this legislation was passed, gun deaths sharply declined.”

Very strong, strong, weak, very week, do not know

  • Very weak. The graph is actually misleading.

  • Main takeaway: We often need to know the context in which the data is being collected and analysed. We should also be careful not to be misleading in our choice of scale of axes, among other design choices.

Part 2: Extracting insight from visualizations [20 mins]

Great resource for selecting the right plot: https://www.data-to-viz.com/ ; encourage you all to consult it when choosing to visualize data.

Case Study 1: Context matters

Case Study 2: A case for pie charts

Part 3: Principles of effective visualizations [20 mins]

We will be filling these principles in together as a class

Make a great plot worse

Instructions: Here is a code chunk that shows an effective visualization. First, copy this code chunk into a new cell. Then, modify it to purposely make this chart “bad” by breaking the principles of effective visualization above. Your final chart still needs to run/compile and it should still produce a plot.

How many of the principles did you manage to break?

Plotly demo [10 mins]

Did you know that you can make interactive graphs and plots in R using the plotly library? We will show you a demo of what plotly is and why it’s useful, and then you can try converting a static ggplot graph into an interactive plotly graph.

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble  2.1.3     ✔ purrr   0.3.2
## ✔ tidyr   1.0.0     ✔ dplyr   0.8.3
## ✔ readr   1.3.1     ✔ stringr 1.4.0
## ✔ tibble  2.1.3     ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(gapminder)

p<-ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, color=continent))+
  geom_point()

p

p%>%ggplotly()
gapminder%>%
  plot_ly(x=~gdpPercap,
          y=~lifeExp,
          color=~continent,
          type='scatter',
          mode='markers')
#Will only work for html, unless you export as an image 

#create an account in plot.ly

Sys.setenv("plotly_username"="")
Sys.setenv("plotly_api_key"="")

api_create(p, filename="")
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need a plotly username. See help(signup, package = 'plotly')
## Warning: Couldn't find username
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need an api_key. See help(signup, package = 'plotly')
## Warning: Couldn't find api_key
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need a plotly username. See help(signup, package = 'plotly')
## Warning: Couldn't find username
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need an api_key. See help(signup, package = 'plotly')
## Warning: Couldn't find api_key
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need a plotly username. See help(signup, package = 'plotly')
## Warning: Couldn't find username
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.credentials': No such file or directory
## Warning in open.connection(con, "rb"): cannot open file '/Users/
## isabelchew/.plotly/.config': No such file or directory
## Warning: You need an api_key. See help(signup, package = 'plotly')
## Warning: Couldn't find api_key
## Error: Client error: (401) Unauthorized
##  No such user

This is a preview of what we’ll be doing in STAT 547 - making dynamic and interactive dashboards using R!

Supplemental worksheet (Optional)

You are highly encouraged to the cm013 supplemental exercises worksheet. It is a great guide that will take you through Scales, Colours, and Themes in ggplot. There is also a short guided activity showing you how to make a ggplot interactive using plotly.